<style>
.divmask {
    pointer-events: none;
}
</style>

<template>
    <div class="auto">
      <!-- <a href="#" class="btn btn-primary" @click="this.$back('items')">&lt;&lt;</a> -->
      <!-- <img src="../../../static/singleleft2.png" width="25" height="25" @click="this.$back('items')"></img> -->
      <!-- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h4>安检相关</h4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
      <span class="glyphicon glyphicon-menu-left" @click="this.$back('items')"></span>
      <span><b>&nbsp;&nbsp;&nbsp;&nbsp;安检相关&nbsp;&nbsp;&nbsp;&nbsp;</b></span>
      <span>
        <a href="#" v-show="actionEnabled" class="btn btn-success" @click="savePaper(true)">保存</a>
        <a href="#" v-show="actionEnabled" class="btn btn-primary" @click="uploadPaper">上传</a>
      </span>
      <div>
        <radio-group :value.sync="selector" type="info" buttons="false">
          <radio value="paper-state">安检概况</radio>
          <radio value="paper-user">用户信息</radio>
          <radio value="paper-device">设备信息</radio>
          <radio value="paper-defect">其他隐患</radio>
          <radio value="device-defect">隐患汇总</radio>
          <radio value="paper-feedback">用户反馈</radio>
        </radio-group>
      </div>
      <route :comp="{name: 'void-page'}" name="paper-router"></route>
      <modal :show.sync="showModal">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">
            照片浏览
          </h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <img :src="pic_to_be_viewed" width="420" height="560">
        </div>
        <div slot="modal-footer" class="modal-footer">
          <button type="button" class="btn btn-success" @click="showModal = false">确认</button>
        </div>
      <modal>
    </div>
</template>

<script>
import RadioGroup from 'vue-strap/src/radioGroup'
import Radio from 'vue-strap/src/radioBtn'
import Vue from 'vue'
import * as Util from '../Util'

export default {
  title: '安检单',
  props: ['f_plan_id', 'item', 'role'],
  data() {
    return {
      data: {
      },
      timer: null,
      showModal: false,
      pic_to_be_viewed: ''
    }
  },
  components: {
    RadioGroup,
    Radio
  },
  computed: {
    selector: {
      get() {
        return 'paper-state'
      },
      set(val) {
        this.$goto(val, {}, 'paper-router')
      }
    },
    actionEnabled: {
      get() {
        return this.role != 'view'
      }
    }
  },
  methods: {
    viewPic(fileName) {
      console.log('view pic: ' + fileName)
      if(this.role == 'view') {
        if(!fileName.endsWith('nopic.png')) {
          this.pic_to_be_viewed = fileName
          this.showModal = true
        }
      } else {
        HostApp.viewPic(fileName)
      }
    },
    tick() {
      this.timer = null
      this.data.f_offsite_time = Util.toStandardTimeString()
      this.timer = window.setTimeout(this.tick, 1000)
    },
    getRepairState() {
      let repaired = true
      this.data.f_defects.forEach((defect) => {
        repaired = repaired & (defect.f_ratification == '已处理')
      })
      return repaired
    },
    verifyRepairDefects() {
      this.data.f_repair_date = Util.toStandardTimeString()
      this.data.f_repaired = this.getRepairState() ? '已修' : '在修'
    },
    savePaper(saveOnly) {
      if(this.role == 'repair') {
        let errorMsg = this.verifyRepairDefects()
        if(errorMsg) {
          HostApp.alert(errorMsg)
          return false
        }
        this.save(saveOnly)
        return true
      } else {
        //保存时不做校验
        let errString = saveOnly ? false : this.errorVerify()
        if(errString) {
          console.log(errString)
          HostApp.alert(errString)
          return false
        } else {
          errString = this.collectDefects()
          if(!saveOnly && errString == 1) {
            //使用HostApp总报错，改为window
            window.__callback__ = this.genuineSave
            window.__this__ = this
            console.log('回调:' + window.__callback__)
            HostApp.confirm('本次安检存在安全隐患，真的不需要指定维修人吗？', 'javascript:window.__callback__(' +saveOnly + ');')
            return
          }
          errString = saveOnly ? false : errString
          //如果上传要检验
          if(errString) {
            console.log(errString)
            HostApp.alert(errString)
            return false
          }
          this.save(saveOnly)
        }
        this.disableTimer()
        return true
      }
    },
    genuineSave(saveOnly) {
      window.__this__.save(saveOnly)
      if(!saveOnly)
        window.__this__.disableTimer()
      window.__callback__ = null
      window.__this__ = null
    },
    uploadPaper() {
      this.savePaper(false)
    },
    errorVerify() {
      let hasError = this.verifyState()
      if(hasError)
        return hasError
      if(this.data.f_entry_status != '入户')
        return null
      hasError = this.verifyUser()
      if(hasError)
        return hasError
      hasError = this.verifyDevices()
      if(hasError)
        return hasError
      hasError = this.verifyDefect()
      if(hasError)
        return hasError
      hasError = this.verifyFeedback()
      if(hasError)
        return hasError
    },
    verifyState() {
      if(Vue.android && this.data.f_entry_status == '未使用天然气' && this.data.f_nongasuser_path == Vue.nopic)
        return '未使用天然气则必须拍照'
      if(Vue.android && this.data.f_entry_status == '到访不遇' && this.data.f_noanswer_path == Vue.nopic)
        return '到访不遇必须拍照'
      if(this.data.f_entry_status != '入户') {
        if(this.data.f_no_gas_device || this.data.f_unload_battery || this.data.f_close_valve)
          return '入户状态和处置方式冲突'
      }
    },
    verifyUser() {
      let prefix = '用户基本信息：'
      if(Util.required(this.data.f_user_name))
        return prefix + '用户名必须输入'
      if(Util.required(this.data.f_user_phone))
        return prefix + '用户电话必须输入'
      if(Util.required(this.data.f_residential_area))
        return prefix + '小区名称必须输入'
      if(Util.required(this.data.f_address))
        return prefix + '地址必须输入'
    },
    verifyMeters() {
      let usage = 0
      //abrupt exit
      for(var i=0; i < this.data.f_meters.length; i++) {
        let item = this.data.f_meters[i]
        let prefix = '燃气表' + (i + 1) + '：'
        if(Vue.android && item.f_meter_path == Vue.nopic)
          return prefix + '缺少燃气表照片'
        if(Util.required(item.f_card_id))
          return prefix + '缺少卡号'
        if(Util.required(item.f_maker))
          return prefix + '缺少生产厂家'
        if(item.f_maker && item.f_maker == '其他' && Util.required(item.f_maker_))
          return prefix + '生产厂家选择其他但未给出'
        if(Util.required(item.f_diaphgram_size))
          return prefix + '缺少表型'
        if(item.f_diaphgram_size && item.f_diaphgram_size == '其他' && Util.required(item.f_diaphgram_size_))
          return prefix + '表型选择其他但未给出'
        if(Util.required(item.f_reading))
          return prefix + '缺少基表数'
        if(!Util.isInt(item.f_reading))
          return prefix + '机表数不是整数'
        if(Util.required(item.f_remaining_gas))
          return prefix + '缺少剩余气量'
        if(Util.required(item.f_prior_reading))
          return prefix + '缺少上期基表读数'
        if(!Util.isInt(item.f_remaining_gas))
          return prefix + '剩余气量不是整数'
        if(Util.required(item.f_meter_no))
          return prefix + '缺少机表号'
        if(Util.required(item.f_usage))
          return prefix + '缺少本期用气量'
        if(item.f_usage / 1 < 0)
            return prefix + '本期用气量不能小于0'
        if(Util.required(item.f_making_date))
          return prefix + '缺少燃气表生产年份'
        if(!Util.isInt(item.f_making_date))
          return prefix + '燃气表生产年份不是整数'
        if(Util.required(item.f_flow_direction))
          return prefix + '缺少进气方向'
        if(Util.required(item.f_defect_no) && Util.required(item.f_defect_wrapped) && Util.required(item.f_defect_leakage))
          return prefix + '缺少安检项'
        if(item.f_invalid && item.f_invalid_path == Vue.nopic)
          return prefix + '报废必须拍照'
        if(item.f_defect_wrapped && item.f_wrapped_path == Vue.nopic)
          return prefix + '包裹必须拍照'
        if(Util.required(item.f_meter_defect))
          return prefix + '缺少表故障选项'
        if(Util.required(item.f_meter_erosion))
          return prefix + '缺少腐蚀选项'
        if(item.f_meter_erosion == '严重' && item.f_erosion_path == Vue.nopic)
          return prefix + '严重腐蚀必须拍照'
        usage += item.f_usage/1
      }
      //本期总用气量
      this.data.f_usage = usage
    },
    verifyPlumbs() {
      //abrupt exit
      for(var i=0; i < this.data.f_plumbs.length; i++) {
        let item = this.data.f_plumbs[i]
        let prefix = '立管' + (i + 1) + '：'
        if(Util.required(item.f_plumb_normal) && Util.required(item.f_plumb_trapped))
          return prefix + '缺少安检项'
        if(item.f_plumb_trapped && item.f_trapped_path == Vue.nopic)
          return prefix + '暗封必须拍照'
        if(Util.required(item.f_plumb_erosion))
          return prefix + '缺少腐蚀选项'
        if(item.f_plumb_erosion == '严重' && item.f_erosion_path == Vue.nopic)
          return prefix + '严重腐蚀必须拍照'
        if(Util.required(item.f_plumb_tightness))
          return prefix + '缺少严密性选项'
        if(Util.required(item.f_plumb_pressuretest))
          return prefix + '缺少静压力测试选项'
        if(Util.required(item.f_plumb_pressure))
          return prefix + '缺少测试压力'
        if(!Util.isInt(item.f_plumb_pressure))
          return prefix + '测试压力不是整数'
      }
    },
    verifyValves() {
      //abrupt exit
      for(var i=0; i < this.data.f_valves.length; i++) {
        let item = this.data.f_valves[i]
        let prefix = '阀门' + (i + 1) + '：'
        if(Util.required(item.f_valve_meter_normal) && Util.required(item.f_valve_meter_leakage) && Util.required(item.f_valve_meter_wrapped))
          return prefix + '缺少表前阀安检项'
        if(item.f_valve_meter_wrapped && item.f_meter_wrapped_path == Vue.nopic)
          return prefix + '表前阀包裹选中则必须拍照'
        if(Util.required(item.f_valve_meter_on))
          return prefix + '缺少表前阀开关状态'
        if(Util.required(item.f_valve_auto_normal) && Util.required(item.f_valve_auto_leakage) && Util.required(item.f_valve_auto_wrapped) && Util.required(item.f_valve_auto_damaged) && Util.required(item.f_valve_auto_non))
          return prefix + '缺少自闭阀安检项'
        if(item.f_valve_auto_wrapped && item.f_auto_wrapped_path == Vue.nopic)
          return prefix + '自闭阀包裹选中则必须拍照'
        if(item.f_valve_auto_damaged && item.f_auto_damaged_path == Vue.nopic)
          return prefix + '自闭阀损坏选中则必须拍照'
        if(item.f_valve_auto_non && item.f_auto_non_path == Vue.nopic)
          return prefix + '自闭阀未安装选中则必须拍照'
        if(Util.required(item.f_valve_cooker_normal) && Util.required(item.f_valve_cooker_leakage) && Util.required(item.f_valve_cooker_wrapped))
          return prefix + '缺少灶前阀安检项'
        if(item.f_valve_cooker_wrapped && item.f_cooker_wrapped_path == Vue.nopic)
          return prefix + '灶前阀包裹选中则必须拍照'
        // if(Util.required(item.f_valve_cooker_on))
        //   return prefix + '缺少未用气状态'
      }
    },
    verifyInnerPlumbs() {
      //abrupt exit
      for(var i=0; i < this.data.f_inner_plumbs.length; i++) {
        let item = this.data.f_inner_plumbs[i]
        let prefix = '户内管' + (i + 1) + '：'
        if(Util.required(item.f_plumb_normal)
          && Util.required(item.f_plumb_leakage)
          && Util.required(item.f_plumb_viaroom)
          && Util.required(item.f_plumb_viabedroom)
          && Util.required(item.f_plumb_nearappliance)
          && Util.required(item.f_plumb_wrapped)
          && Util.required(item.f_plumb_reroute)
          && Util.required(item.f_plumb_kitchen_misuse)
        )
          return prefix + '缺少安检项'
        if(item.f_plumb_viaroom && item.f_viaroom_path == Vue.nopic)
          return prefix + '穿客厅选中则必须拍照'
        if(item.f_plumb_viabedroom && item.f_viabedroom_path == Vue.nopic)
          return prefix + '穿卧室选中则必须拍照'
        if(item.f_plumb_nearappliance && item.f_plumb_nearappliance_path == Vue.nopic)
          return prefix + '与电器过近选中则必须拍照'
        if(item.f_plumb_wrapped && item.f_wrapped_path == Vue.nopic)
          return prefix + '暗封选中则必须拍照'
        if(item.f_plumb_reroute && item.f_reroute_path == Vue.nopic)
          return prefix + '私改选中则必须拍照'
        if(item.f_plumb_kitchen_misuse && item.f_misuse_path == Vue.nopic)
          return prefix + '厨房他用选中则必须拍照'
      }
    },
    verifyAlarms() {
      //abrupt exit
      for(var i=0; i < this.data.f_alarms.length; i++) {
        let item = this.data.f_alarms[i]
        let prefix = '报警器' + (i + 1) + '：'
        if(Util.required(item.f_alarm_inuse))
          return prefix + '缺少使用选项'
        if(item.f_alarm_inuse == '未使用')
          continue
        if(Util.required(item.f_mechanic))
          return prefix + '缺少类型选项'
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌选项'
        if(Util.required(item.f_model))
          return prefix + '缺少型号选项'
      }
    },
    verifyCookers() {
      //abrupt exit
      for(var i=0; i < this.data.f_cookers.length; i++) {
        let item = this.data.f_cookers[i]
        let prefix = '灶具' + (i + 1) + '：'
        if(Vue.android && item.f_pic1_path == Vue.nopic)
          return prefix + '缺少灶具照片'
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌'
        if(Util.required(item.f_making_date))
          return prefix + '缺少出厂日期'
        if(Util.required(item.f_normal)
          && Util.required(item.f_damaged)
          && Util.required(item.f_gas_mismatch)
          && Util.required(item.f_valid)
          && Util.required(item.f_nearwall)
        )
          return prefix + '缺少安检项'
        if(item.f_gas_mismatch && item.f_mismatch_path == Vue.nopic)
          return prefix + '气源不匹配选中则必须拍照'
        if(item.f_damaged && item.f_damaged_path == Vue.nopic)
          return prefix + '损坏选中则必须拍照'
        if(item.f_valid && item.f_valid_path == Vue.nopic)
          return prefix + '报废选中则必须拍照'
        if(item.f_nearwall && item.f_nearwall_path == Vue.nopic)
          return prefix + '与墙面距离不足选中则必须拍照'
        if(Util.required(item.f_setup_state))
          return prefix + '缺少灶具形式'
        if(Util.required(item.f_hasoffprotect))
          return prefix + '缺少熄火保护'
        if(item.f_hasoffprotect == '无' && item.f_hasoffprotect_path == Vue.nopic)
          return prefix + '无熄火保护则必须拍照'
        if(item.f_hasoffprotect != '无') {
          if(Util.required(item.f_offprotect_valid))
            return prefix + '缺少熄火保护是否有效'
          if(item.f_offprotect_valid == '无效' && item.f_offprotect_valid_path == Vue.nopic)
            return prefix + '熄火保护无效则必须拍照'
        }
        if(Util.required(item.f_pipe_ok)
          && Util.required(item.f_pipe_leakage)
          && Util.required(item.f_pipe_worn)
          && Util.required(item.f_pipe_mutilated)
        )
          return prefix + '缺少软管安检项'
        if(item.f_pipe_worn && item.f_worn_path == Vue.nopic)
          return prefix + '软管老化则必须拍照'
        if(item.f_pipe_mutilated && item.f_mutilated_path == Vue.nopic)
          return prefix + '软管破损则必须拍照'
        if(Util.required(item.f_pipe_material))
          return prefix + '缺少软管材质'
        if(item.f_pipe_material == '橡胶' && item.f_material_path == Vue.nopic)
          return prefix + '软管材质为橡胶必须拍照'
        if(item.f_pipe_no_fasten && item.f_no_fasten_path == Vue.nopic)
          return prefix + '软管无管卡必须拍照'
        if(Util.required(item.f_pipe_fasten))
          return prefix + '缺少管卡数'
        if(!Util.isInt(item.f_pipe_fasten))
          return prefix + '管卡数不是整数'
        if(Util.required(item.f_pipe_length2))
          return prefix + '缺少软管长度'
        // if(!Util.isInt(item.f_pipe_length2))
        //   return prefix + '软管长度不是整数'
      }
    },
    verifyHeaters() {
      //abrupt exit
      for(var i=0; i < this.data.f_heaters.length; i++) {
        let item = this.data.f_heaters[i]
        let prefix = '热水器' + (i + 1) + '：'
        if(Vue.android && item.f_pic1_path == Vue.nopic)
          return prefix + '缺少热水器照片'
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌'
        if(Util.required(item.f_making_date))
          return prefix + '缺少出厂日期'
        if(Util.required(item.f_pressure))
          return prefix + '缺少工作压力'
        if(!Util.isFloat(item.f_pressure))
          return prefix + '工作压力不是数字'
        if(Util.required(item.f_pipe_ok)
          && Util.required(item.f_pipe_leakage)
          && Util.required(item.f_pipe_worn)
        )
          return prefix + '缺少软管安检项'
        if(item.f_pipe_worn && item.f_worn_path == Vue.nopic)
          return prefix + '软管老化则必须拍照'
        if(Util.required(item.f_pipe_material))
          return prefix + '缺少软管材质'
        if(item.f_pipe_material == '橡胶' && item.f_material_path == Vue.nopic)
          return prefix + '橡胶软管必须拍照'
        if(item.f_pipe_no_fasten && item.f_no_fasten_path == Vue.nopic)
          return prefix + '软管无管卡必须拍照'
        if(Util.required(item.f_pipe_fasten))
          return prefix + '缺少管卡数'
        if(!Util.isInt(item.f_pipe_fasten))
          return prefix + '管卡数不是整数'
        if(Util.required(item.f_pipe_length2))
          return prefix + '缺少软管长度'
        // if(!Util.isInt(item.f_pipe_length2))
        //   return prefix + '软管长度不是整数'
        if(Util.required(item.f_vent_balanced)
          && Util.required(item.f_vent_forced)
          && Util.required(item.f_vent_exist)
          && Util.required(item.f_vent_direct)
          && Util.required(item.f_vent_damaged)
          && Util.required(item.f_vent_direct_heater)
          && Util.required(item.f_vent_trapped)
          && Util.required(item.f_vent_notsealed)
        )
          return prefix + '缺少烟道安检项'
        if(item.f_vent_direct_heater && item.f_direct_heater_path == Vue.nopic)
          return prefix + '直排热水器必须拍照'
        if(item.f_vent_trapped && item.f_vent_trapped_path == Vue.nopic)
          return prefix + '烟道未接到室外必须拍照'
        if(item.f_vent_notsealed && item.f_vent_notsealed_path == Vue.nopic)
          return prefix + '烟道与墙体未密封必须拍照'
        if(item.f_vent_damaged && item.f_vent_damaged_path == Vue.nopic)
          return prefix + '烟道破损必须拍照'
      }
    },
    verifyFurnaces() {
      //abrupt exit
      for(var i=0; i < this.data.f_furnaces.length; i++) {
        let item = this.data.f_furnaces[i]
        let prefix = '壁挂锅炉' + (i + 1) + '：'
        if(Vue.android && item.f_pic1_path == Vue.nopic)
          return prefix + '缺少壁挂炉照片'
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌'
        if(Util.required(item.f_making_date))
          return prefix + '缺少出厂日期'
        if(Util.required(item.f_pressure_label))
          return prefix + '缺少工作压力'
        if(!Util.isFloat(item.f_pressure_label))
          return prefix + '工作压力不是数字'
        if(Util.required(item.f_pipe_ok)
          && Util.required(item.f_pipe_leakage)
          && Util.required(item.f_pipe_worn)
        )
          return prefix + '缺少软管安检项'
        if(item.f_pipe_worn && item.f_worn_path == Vue.nopic)
          return prefix + '软管老化则必须拍照'
        if(Util.required(item.f_pipe_material))
          return prefix + '缺少软管材质'
        if(item.f_pipe_material == '橡胶' && item.f_material_path == Vue.nopic)
          return prefix + '橡胶软管必须拍照'
        if(item.f_pipe_no_fasten && item.f_no_fasten_path == Vue.nopic)
          return prefix + '软管无管卡必须拍照'
        if(Util.required(item.f_pipe_fasten))
          return prefix + '缺少管卡数'
        if(!Util.isInt(item.f_pipe_fasten))
          return prefix + '管卡数不是整数'
        if(Util.required(item.f_pipe_length2))
          return prefix + '缺少软管长度'
        // if(!Util.isInt(item.f_pipe_length2))
        //   return prefix + '软管长度不是整数'
        if(Util.required(item.f_vent_horizontal)
          && Util.required(item.f_vent_tilted)
          && Util.required(item.f_vent_balanced)
          && Util.required(item.f_vent_forced)
          && Util.required(item.f_vent_trapped)
          && Util.required(item.f_vent_damaged)
          && Util.required(item.f_vent_notsealed)
          && Util.required(item.f_vent_no)
        )
          return prefix + '缺少烟道安检项'
        if(item.f_vent_no && item.f_vent_no_path == Vue.nopic)
          return prefix + '未安装烟道必须拍照'
        if(item.f_vent_notsealed && item.f_vent_notsealed_path == Vue.nopic)
          return prefix + '烟道与墙体未密封必须拍照'
        if(item.f_vent_damaged && item.f_vent_damaged_path == Vue.nopic)
          return prefix + '烟道破损必须拍照'
      }
    },
    verifyDevices() {
      if(!this.data.f_meters || this.data.f_meters.length == 0)
        return '请检查燃气表'
      let hasError = null
      if(this.data.f_meters && this.data.f_meters.length > 0)
        hasError = hasError || this.verifyMeters()
      if(hasError)
        return hasError
      if(!this.data.f_plumbs || this.data.f_plumbs.length == 0)
        return '请检查立管'
      if(this.data.f_plumbs && this.data.f_plumbs.length > 0)
        hasError = hasError || this.verifyPlumbs()
      if(hasError)
        return hasError
      if(!this.data.f_valves || this.data.f_valves.length == 0)
        return '请检查阀门'
      if(this.data.f_valves && this.data.f_valves.length > 0)
        hasError = hasError || this.verifyValves()
      if(hasError)
        return hasError
      if(!this.data.f_inner_plumbs || this.data.f_inner_plumbs.length == 0)
        return '请检查户内管'
      if(this.data.f_inner_plumbs && this.data.f_inner_plumbs.length > 0)
        hasError = hasError || this.verifyInnerPlumbs()
      if(hasError)
        return hasError
      // if(!this.data.f_alarms || this.data.f_alarms.length == 0)
      //   return '请检查报警器'
      if(this.data.f_alarms && this.data.f_alarms.length > 0)
        hasError = hasError || this.verifyAlarms()
      if(hasError)
        return hasError
      if(this.data.f_cookers && this.data.f_cookers.length > 0)
        hasError = hasError || this.verifyCookers()
      if(hasError)
        return hasError
      if(this.data.f_heaters && this.data.f_heaters.length > 0)
        hasError = hasError || this.verifyHeaters()
      if(hasError)
        return hasError
      if(this.data.f_furnaces && this.data.f_furnaces.length > 0)
        hasError = hasError || this.verifyFurnaces()
      if(hasError)
        return hasError
    },
    verifyDefect() {
      let prefix = '全局隐患：'
      if(Vue.android && this.data.f_defect_cramped && this.data.f_cramped_path == Vue.nopic)
        return prefix + '通风不良若选中则必须拍照'
      if(Vue.android && this.data.f_defect_bedroom && this.data.f_bedroom_path == Vue.nopic)
        return prefix + '燃气设施安装在卧室/卫生间若选中则必须拍照'
      if(Vue.android && this.data.f_defect_longpipe && this.data.f_longpipe_path == Vue.nopic)
        return prefix + '软管过长若选中则必须拍照'
      if(Vue.android && this.data.f_defect_firesource && this.data.f_firesource_path == Vue.nopic)
        return prefix + '第二火源若选中则必须拍照'
      if(Vue.android && this.data.f_defect_explosive && this.data.f_explosive_path == Vue.nopic)
        return prefix + '燃气设施房间堆放易燃易爆品若选中则必须拍照'
      if(Vue.android && this.data.f_defect_viacabinet && this.data.f_viacabinet_path == Vue.nopic)
        return prefix + '软管穿柜/门窗/顶棚若选中则必须拍照'
      if(Vue.android && this.data.f_defect_intermittent && this.data.f_intermittent_path == Vue.nopic)
        return prefix + '软管有接头若选中则必须拍照'
      if(Vue.android && this.data.f_defect_nearfire && this.data.f_nearfire_path == Vue.nopic)
        return prefix + '软管离火源过近若选中则必须拍照'
      if(Vue.android && this.data.f_defect_openkitchen && this.data.f_openkitchen_path == Vue.nopic)
        return prefix + '开放厨房若选中则必须拍照'
      if(Vue.android && this.data.f_defect_hang && this.data.f_hang_path == Vue.nopic)
        return prefix + '挂物若选中则必须拍照'
      if(Vue.android && this.data.f_defect_notfixed && this.data.f_notfixed_path == Vue.nopic)
        return prefix + '燃气设备固定不牢靠若选中则必须拍照'
    },
    verifyFeedback() {
      let prefix = '用户反馈：'
      if(Vue.android && (!this.data.f_overall_path || this.data.f_overall_path == Vue.nopic) && this.data.f_entry_status == '入户')
          return prefix + '必须有安检总体照片'
      if(Vue.android && this.data.f_defect_notified && this.data.f_notified_path == Vue.nopic)
        return prefix + '下发安检通知书则必须给通知书拍照'
      if(!this.data.f_client_evaluation && this.data.f_entry_status == '入户')
          return prefix + '请选择服务评价'
      if(Vue.android && !this.data.f_recording1_path && this.data.f_entry_status == '入户')
          return prefix + '安检必须录音'
    },
    collectDefects() {
      // 维修时，不再提取数据了
      if(this.role != 'inspect')
        return
      this.$set('data.f_defects', [])
      let defects = this.data.f_defects
      // 全局隐患
      if(this.data.f_defect_cramped)
        defects.push({f_defect: '通风不良', f_defect_level: 'C3', f_defect_path: this.data.f_cramped_path})
      if(this.data.f_defect_longpipe)
        defects.push({f_defect: '软管超长', f_defect_level: 'C7', f_defect_path: this.data.f_longpipe_path})
      if(this.data.f_defect_firesource)
        defects.push({f_defect: '第二火源', f_defect_level: 'A11', f_defect_path: this.data.f_firesource_path})
      if(this.data.f_defect_explosive)
        defects.push({f_defect: '燃气设施房间堆放易燃易爆品', f_defect_level: 'C5', f_defect_path: this.data.f_explosive_path})
      if(this.data.f_defect_viacabinet)
        defects.push({f_defect: '软管穿柜/门窗/顶棚', f_defect_level: 'C9', f_defect_path: this.data.f_viacabinet_path})
      if(this.data.f_defect_intermittent)
        defects.push({f_defect: '软管有接头', f_defect_level: 'A4', f_defect_path: this.data.f_intermittent_path})
      if(this.data.f_defect_nearfire)
        defects.push({f_defect: '软管离火源过近', f_defect_level: 'B8', f_defect_path: this.data.f_nearfire_path})
      if(this.data.f_defect_openkitchen)
        defects.push({f_defect: '开放厨房', f_defect_level: 'B6', f_defect_path: this.data.f_openkitchen_path})
      if(this.data.f_defect_bedroom)
        defects.push({f_defect: '燃气设施安装在卧室/卫生间', f_defect_level: 'B18', f_defect_path: this.data.f_bedroom_path})
      if(this.data.f_defect_hang)
        defects.push({f_defect: '燃气管道/燃气表搭挂重物', f_defect_level: 'C15', f_defect_path: this.data.f_hang_path})
      if(this.data.f_defect_notfixed)
        defects.push({f_defect: '燃气设施固定不牢靠', f_defect_level: 'C11', f_defect_path: this.data.f_notfixed_path})
      // 燃气表隐患
      if(this.data.f_meters) {
        this.data.f_meters.forEach(function(item, index) {
          let pre = '燃气表' + (index+1)
          if(item.f_defect_wrapped)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '包裹', f_defect_level: 'C13', f_defect_path: item.f_meter_path})
          if(item.f_defect_leakage)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_meter_erosion && item.f_meter_erosion == '严重')
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '严重腐蚀', f_defect_level: 'C2', f_defect_path: item.f_erosion_path})
          if(item.f_invalid)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '报废', f_defect_level: 'C17', f_defect_path: item.f_invalid_path})
        })
      }
      // 立管
      if(this.data.f_plumbs) {
        this.data.f_plumbs.forEach(function(item, index) {
          let pre = '立管' + (index+1)
          if(item.f_plumb_trapped)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '暗封', f_defect_level: 'C12', f_defect_path: item.f_trapped_path})
          if(item.f_plumb_erosion && item.f_plumb_erosion == '严重')
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '严重腐蚀', f_defect_level: 'C1', f_defect_path: item.f_erosion_path})
        })
      }
      // 阀门
      if(this.data.f_valves) {
        this.data.f_valves.forEach(function(item, index) {
          let pre = '阀门' + (index+1)
          if(item.f_valve_meter_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '表前阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_meter_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '表前阀', f_defect: pre + '包裹', f_defect_level: 'B16', f_defect_path: item.f_meter_wrapped_path})
          if(item.f_valve_auto_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '自闭阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_auto_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '自闭阀', f_defect: pre + '包裹', f_defect_level: 'B16', f_defect_path: item.f_auto_wrapped_path})
          if(item.f_valve_auto_damaged)
            defects.push({f_device_type: '阀门', f_device_id: '自闭阀', f_defect: pre + '损坏', f_defect_level: 'A7', f_defect_path: item.f_auto_damaged_path})
          if(item.f_valve_auto_non)
            defects.push({f_device_type: '阀门', f_device_id: '自闭阀', f_defect: pre + '未安装', f_defect_level: 'A6', f_defect_path: item.f_auto_non_path})
          if(item.f_valve_cooker_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '灶前阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_cooker_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '灶前阀', f_defect: pre + '包裹', f_defect_level: 'B16', f_defect_path: item.f_cooker_wrapped_path})
        })
      }
      // 户内管
      if(this.data.f_inner_plumbs) {
        this.data.f_inner_plumbs.forEach(function(item, index) {
          let pre = '户内管' + (index+1)
          if(item.f_plumb_viaroom)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '穿客厅', f_defect_level: 'A1', f_defect_path: item.f_viaroom_path})
          if(item.f_plumb_viabedroom)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '穿卧室', f_defect_level: 'B16', f_defect_path: item.f_viabedroom_path})
          if(item.f_plumb_nearappliance)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '与电器过近', f_defect_level: 'C20', f_defect_path: item.f_plumb_nearappliance_path})
          if(item.f_plumb_wrapped)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '暗封', f_defect_level: 'A1', f_defect_path: item.f_wrapped_path})
          if(item.f_plumb_reroute)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '私改', f_defect_level: 'B16', f_defect_path: item.f_reroute_path})
          if(item.f_plumb_kitchen_misuse)
            defects.push({f_device_type: '户内管', f_device_id: pre, f_defect: pre + '厨房他用', f_defect_level: 'A7', f_defect_path: item.f_misuse_path})
        })
      }
      // 报警器
      // 灶具
      if(this.data.f_cookers) {
        this.data.f_cookers.forEach(function(item, index) {
          let pre = '灶具' + (index+1)
          if(item.f_gas_mismatch)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '气源不匹配', f_defect_level: 'C20', f_defect_path: item.f_mismatch_path})
          if(item.f_damaged)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '损坏', f_defect_level: 'C19', f_defect_path: item.f_damaged_path})
          if(item.f_valid)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '报废', f_defect_level: 'C18', f_defect_path: item.f_valid_path})
          if(item.f_nearwall)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '与墙面距离不足', f_defect_level: 'B16', f_defect_path: item.f_nearwall_path})
          if(item.f_hasoffprotect && item.f_hasoffprotect == '无')
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '无熄火保护', f_defect_level: 'B10', f_defect_path: item.f_hasoffprotect_path})
          if(item.f_offprotect_valid && item.f_offprotect_valid == '无效')
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '熄火保护无效', f_defect_level: 'B11', f_defect_path: item.f_offprotect_valid_path})
          if(item.f_pipe_material && item.f_pipe_material == '橡胶')
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '软管材质为橡胶', f_defect_level: 'C6', f_defect_path: item.f_material_path})
          if(item.f_pipe_leakage)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '软管漏气', f_defect_level: 'A1'})
          if(item.f_pipe_worn)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '软管老化', f_defect_level: 'C10', f_defect_path: item.f_worn_path})
          if(item.f_pipe_mutilated)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '软管破损', f_defect_level: 'A3', f_defect_path: item.f_mutilated_path})
          if(item.f_pipe_no_fasten)
            defects.push({f_device_type: '灶具', f_device_id: pre, f_defect: pre + '软管无管卡', f_defect_level: 'A5', f_defect_path: item.f_no_fasten_path})
        })
      }
      // 热水器
      if(this.data.f_heaters) {
        this.data.f_heaters.forEach(function(item, index) {
          let pre = '热水器' + (index+1)
          if(item.f_pipe_material && item.f_pipe_material == '橡胶')
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '软管材质为橡胶', f_defect_level: 'C6', f_defect_path: item.f_material_path})
          if(item.f_pipe_leakage)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '软管漏气', f_defect_level: 'A1'})
          if(item.f_pipe_worn)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '软管老化', f_defect_level: 'C10', f_defect_path: item.f_worn_path})
          if(item.f_pipe_no_fasten)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '软管无管卡', f_defect_level: 'A5', f_defect_path: item.f_no_fasten_path})
          if(item.f_vent_direct_heater)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '直排热水器', f_defect_level: 'A2', f_defect_path: item.f_direct_heater_path})
          if(item.f_vent_trapped)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '烟道未接到室外', f_defect_level: 'B2', f_defect_path: item.f_vent_trapped_path})
          if(item.f_vent_damaged)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '烟道破损', f_defect_level: 'B1', f_defect_path: item.f_vent_damaged_path})
          if(item.f_vent_notsealed)
            defects.push({f_device_type: '热水器', f_device_id: pre, f_defect: pre + '与墙体间未密封', f_defect_level: 'B3', f_defect_path: item.f_vent_notsealed_path})
        })
      }
      // 壁挂炉
      if(this.data.f_furnaces) {
        this.data.f_furnaces.forEach(function(item, index) {
          let pre = '壁挂炉' + (index+1)
          if(item.f_pipe_material && item.f_pipe_material == '橡胶')
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '软管材质为橡胶', f_defect_level: 'C6', f_defect_path: item.f_material_path})
          if(item.f_pipe_leakage)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '软管漏气', f_defect_level: 'A1'})
          if(item.f_pipe_worn)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '软管老化', f_defect_level: 'C10', f_defect_path: item.f_worn_path})
          if(item.f_pipe_no_fasten)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '软管无管卡', f_defect_level: 'A5', f_defect_path: item.f_no_fasten_path})
          if(item.f_vent_no)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '未安装烟道', f_defect_level: 'A2', f_defect_path: item.f_vent_no_path})
          if(item.f_vent_damaged)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '烟道破损', f_defect_level: 'B1', f_defect_path: item.f_vent_damaged_path})
          if(item.f_vent_notsealed)
            defects.push({f_device_type: '壁挂炉', f_device_id: pre, f_defect: pre + '与墙体间未密封', f_defect_level: 'B3', f_defect_path: item.f_vent_notsealed_path})
        })
      }
      this.setUrgency()
      if(defects.length >0) {
        if(!this.data.f_repairman) {
          return 1
        }
      } else {
        if(this.data.f_repairman) {
          return "不存在安全隐患，不需要指定维修人"
        }
      }
    },
    setUrgency() {
      this.data.f_defect_urgency = 'Z'
      let that = this
      this.data.f_defects.forEach(function(defect) {
        if(that.data.f_defect_urgency > defect.f_defect_level.charAt(0))
          that.data.f_defect_urgency = defect.f_defect_level.charAt(0)
      })
      if(this.data.f_defect_urgency == 'Z')
        this.data.f_defect_urgency = null
    },
    save(saveOnly) {
      if(Vue.android) {
        //let data = {"f_check_item_id":"15","f_check_plan_id":"4028811d569c3f7201569c3ffd86000a","f_checker_id":"5858637","f_checker_name":"安检员1","f_check_type":"民用安检","f_onsite_time":"2016-09-17 10:13:56","f_offsite_time":"2016-09-17 10:15:12","f_entry_status":"入户","f_user_name":"q","f_user_phone":"q","f_user_id":"1","f_address":"街道1小区11号楼1单元层11号","f_no_gas_device":false,"f_close_valve":false,"f_unload_battery":false,"f_room_style":"多层","f_heating_style":"热力公司集中供暖","f_nongasuser_path":"file:\/\/\/android_asset\/nopic.png","f_residential_area":"q","f_meters":[{"f_meter_path":"file:\/\/\/storage\/emulated\/0\/safecheck\/undefined.jpg?0.6854880177415907","f_erosion_path":"file:\/\/\/android_asset\/nopic.png","f_wrapped_path":"file:\/\/\/android_asset\/nopic.png","f_card_id":"qq","f_maker":"成都秦川","f_diaphgram_size":"G1.6","f_diaphgram_size_":"","f_reading":1,"f_remaining_gas":1,"f_meter_no":"1","f_making_date":1111,"f_flow_direction":"左表","f_defect_no":true,"f_meter_defect":"正常","f_meter_erosion":"正常"}],"f_plumbs":[{"f_erosion_path":"file:\/\/\/android_asset\/nopic.png","f_trapped_path":"file:\/\/\/android_asset\/nopic.png","f_plumb_normal":true,"f_plumb_erosion":"正常","f_plumb_tightness":"正常","f_plumb_pressuretest":"不合格","f_plumb_pressure":111}],"f_valves":[{"f_valve_meter_normal":true,"f_valve_meter_on":"开","f_valve_auto_normal":true,"f_valve_cooker_normal":true,"f_valve_cooker_on":"开"}],"f_inner_plumbs":[{"f_plumb_normal":true}],"f_alarms":[{"f_alarm_inuse":"未使用","f_mechanic":"电磁","f_brand":"qq","f_model":"qq"}],"f_cookers":[],"f_furnaces":[],"f_heaters":[],"f_notified_path":"file:\/\/\/android_asset\/nopic.png","f_signature_path":"file:\/\/\/android_asset\/nopic.png","f_client_evaluation":"满意","f_recording1_path":"file:\/\/\/storage\/emulated\/0\/safecheck\/15.amr","f_defects":[]}
        //HostApp.simpleTask({type:'logic', hint: '正在保存安检单，请稍等...', data: {alias:'保存安检单', data: {f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, row: data}}})
        if(saveOnly)
          HostApp.savePaper({hint: ['正在保存工单，请稍等...', '保存成功！', '保存失败！'], data: {f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, row: this.data}})
        else {
          if(this.role == 'repair') {
            HostApp.uploadRepair({f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, data: this.data})
          } else {
            HostApp.uploadPaper({f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, data: this.data})
          }
        }
      }
    },
    disableTimer() {
      if(this.timer) {
        window.clearTimeout(this.timer)
        this.timer = null
      }
    },
    loaded(paper) {
      if(!paper.hasOwnProperty('id')) {
        HostApp.alert('该安检单在手机上已经不存在，可能是卸载过程序。可以进入重新安检，或返回安检计划。')
        this.init()
      } else {
        Util.booleanFilter(paper)
        HostApp.__this__.$set('data', paper)
        HostApp.__this__.$goto('paper-state', {}, 'paper-router')
        console.log(JSON.stringify(paper))
        // 如果打回重检则重新计时
        console.log('HostApp.__this__.role' + HostApp.__this__.role + ' HostApp.__this__.f_approved:' + HostApp.__this__.data.f_approved)
        if(HostApp.__this__.role == 'inspect'
          && HostApp.__this__.data.f_approved
          && HostApp.__this__.data.f_approved == '打回') {
          console.log('打回')
          HostApp.__this__.data.f_onsite_time = Util.toStandardTimeString()
          HostApp.__this__.tick()
        }
      }
      HostApp.__callback__ = null
      HostApp.__this__ = null
    },
    init () {
      this.data.f_check_item_id = this.item.id
      this.data.f_check_plan_id = this.f_plan_id
      this.data.f_checker_id = Vue.user.id
      this.data.f_checker_name = Vue.user.name
      this.data.f_check_type = this.item.f_user_type
      this.data.f_residential_area = this.item.f_residential_area
      this.data.f_onsite_time = Util.toStandardTimeString()
      this.data.f_subcompany = this.item.f_subcompany
      this.data.f_approved = this.item.f_approved
      this.data.f_repair_approved = this.item.f_repair_approved
      this.$set('data.f_offsite_time', Util.toStandardTimeString())

      // 安检状态
      this.$set('data.f_entry_status', '入户')

      // 用户信息
      this.$set('data.f_user_name', this.item.f_user_name)
      this.$set('data.f_user_phone', this.item.f_user_phone)
      this.$set('data.f_user_id', this.item.f_user_id)
      this.$set('data.f_address', this.item.f_address)
      this.$set('data.f_card_id', this.item.f_card_id)

      // 处置方式
      this.$set('data.f_no_gas_device', false)
      this.$set('data.f_close_valve', false)
      this.$set('data.f_unload_battery', false)

      //房屋结构
      this.$set('data.f_room_style', '多层')
      //供暖方式
      this.$set('data.f_heating_style', '热力公司集中供暖')

      this.tick()
      this.$goto('paper-state', {}, 'paper-router')
    }
  },
  ready() {
    //如果已检，则提取安检单
    if(this.item.f_state != '未检') {
      if(this.role != 'view') {
        HostApp.__callback__ = this.loaded
        HostApp.__this__ = this
      }
      //维修单
      if(this.role == 'repair')
        HostApp.fetchPaper({hint: ['正在提取工单，请稍等...', null, '提取工单失败，该工单可能已经处理并上传！'], callback:'javascript:HostApp.__callback__(%s)', data: {f_check_item_id:this.item.f_check_item_id, f_checker_id: this.item.f_checker_id}})
      else if(this.role == 'inspect')
        HostApp.fetchPaper({hint: ['正在提取工单，请稍等...', null, '提取工单失败，该工单可能已经处理并上传！'], callback:'javascript:HostApp.__callback__(%s)', data: {f_check_item_id:this.item.id, f_checker_id: Vue.user.id}})
      else if(this.role == 'view') {
        let that = this
        this.$post('rs/logic/提取安检单', {data:{id: this.item.id}}, {resolveMsg: null, rejectMsg: null}).then(
          (response) => {
            that.$set('data', response.data)
            Util.booleanFilter(that.data)
            Util.ratifyPics(that.data)
            that.$goto('paper-state', {}, 'paper-router')
          }
        ).catch(
          (response) => {
            window.alert('查找安检单失败')
          }
        )
      }
    }
    else {
      this.init()
    }
  },
  destroyed() {
    this.disableTimer()
  }
}
</script>
